﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace EHR.Ctrls.QueryDesign
{
    public partial class FilterPanel : UserControl
    {
        public event EventHandler ConditionPrepared;

        private bool _queryButtonVisible = true;

        private FilterItemBase _editStateFilterItem = null;

        public FilterItemBase EditStateFilterItem
        {
            get { return _editStateFilterItem; }
            set 
            {
                _editStateFilterItem = value;
                IsEditState = _editStateFilterItem != null;
                if (value == null)
                {
                    if(ConditionPrepared != null)
                    {
                        ConditionPrepared(this, new EventArgs());
                    }
                }
            }
        }

        public IList<ColInfo> Columns { get; set; }

        public bool IsEditState
        {
            get { return (bool)GetValue(IsEditStateProperty); }
            set { SetValue(IsEditStateProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsEditState.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsEditStateProperty =
            DependencyProperty.Register("IsEditState", typeof(bool), typeof(FilterPanel), new PropertyMetadata(false));

        public FilterPanel()
        {
            InitializeComponent();

            LayoutRoot.DataContext = this;
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            AddFilter();
        }

        private void AddFilter()
        {
            //保存原焦点项
            if (EditStateFilterItem != null)
            {
                //如果保存不成功则return
                if (!EditStateFilterItem.Validate())
                {
                    return;
                }
            }

            //加入逻辑运算符
            if (itemsPanel.Children.Count != 0)
            {
                LogicOperatorItem op = new LogicOperatorItem();
                AddFilterItemBase(op);
            }

            //加入新项
            FilterItem fi = new FilterItem() { IsEditState = true };
            fi.SetColumns(Columns);
            fi.Deleting += new EventHandler(fi_Deleting);
            EditStateFilterItem = fi;
            AddFilterItemBase(fi);
        }

        void fi_Deleting(object sender, EventArgs e)
        {
            //删除它与它的前一项（ADN或者OR）
            int index = itemsPanel.Children.IndexOf(sender as UIElement);
            itemsPanel.Children.RemoveAt(index);
            if (index == 0)
            {
                if (itemsPanel.Children.Count > 0)
                {
                    itemsPanel.Children.RemoveAt(0);
                }
            }
            else
            {
                itemsPanel.Children.RemoveAt(index - 1);
            }
            EditStateFilterItem = null;
        }

        private void AddFilterItemBase(FilterItemBase item)
        {
            item.Clicked += new EventHandler(item_Clicked);
            item.Saved += new EventHandler(item_Saved);
            itemsPanel.Children.Add(item);
        }

        void item_Clicked(object sender, EventArgs e)
        {
            //保存原焦点项
            if (EditStateFilterItem != null)
            {
                //如果保存不成功则return
                if (!EditStateFilterItem.Validate())
                {
                    return;
                }

                EditStateFilterItem.IsEditState = false;
            }

            EditStateFilterItem = sender as FilterItemBase;
            EditStateFilterItem.IsEditState = true;
        }

        void item_Saved(object sender, EventArgs e)
        {
            EditStateFilterItem = null;
        }

        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {

        }

        public void AddFilter(FilterInfo filterInfo)
        {
            if (itemsPanel.Children.Count != 0)
            {
                LogicOperatorItem op = new LogicOperatorItem() { IsAnd = filterInfo.IsAnd };
                AddFilterItemBase(op);
            }

            //加入新项
            FilterItem fi = new FilterItem();
            fi.SetColumns(Columns);
            fi.SetFilterInfo(filterInfo);
            fi.Deleting += new EventHandler(fi_Deleting);
            AddFilterItemBase(fi);
        }

        public IList<FilterInfo> GetFilters()
        {
            IList<FilterInfo> list = new List<FilterInfo>();
            int count = (itemsPanel.Children.Count + 1) / 2;//条件个数
            for (int i = 0; i < count; i++)
            {
                int index = i * 2;
                FilterItem item = itemsPanel.Children[index] as FilterItem;
                FilterInfo info = item.GetFilterInfo();
                int opIndex = index - 1;
                if (opIndex > 0)
                {
                    info.IsAnd = (itemsPanel.Children[opIndex] as LogicOperatorItem).IsAnd;
                }
                list.Add(info);
            }
            return list;
        }

        public bool Validate()
        {
            if (EditStateFilterItem != null)
            {
                if (!EditStateFilterItem.Validate())
                {
                    return false;
                }

                EditStateFilterItem.IsEditState = false;
                EditStateFilterItem = null;
            }

            return true;
        }

        public void Clear()
        {
            itemsPanel.Children.Clear();

            IsEditState = false;
        }
    }
}
